/**
 * @Copyright:Copyright (c) 2007 - 2023
 */
package com.tengtong.commons.lang;

/**
 * 异常工具类
 *
 * @author lin_jinming
 * @since: v1.0
 */
public class ExceptionUtils {

    /**
     * 创建一个 RuntimeException
     *
     * @param message
     * @return
     */
    public static RuntimeException ofRuntime(String message, Object...objects) {
        String msg = StringUtils.sprintfByBraces(message, objects);
        return new RuntimeException(msg);
    }

    /**
     * 创建一个 RuntimeException
     *
     * @param e
     * @param message
     * @param objects
     * @return
     */
    public static RuntimeException ofRuntime(Throwable e, String message, Object...objects) {
        String msg = StringUtils.sprintfByBraces(message, objects);
        
        return new RuntimeException(msg, e);
    }
    
    /**
     * 如果异常 e 是一个 RuntimeException 就返回 e，否则使用 message 和 objects 创建一个 RuntimeException
     *
     * @param e
     * @param message
     * @param objects
     * @return
     */
    public static RuntimeException ofOrElse(Throwable e, String message, Object...objects) {
        if(e instanceof RuntimeException re) {
            return re;
        }
        return ofRuntime(message, objects);
    }
}
